package com.cqeec.waimaidemo.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cqeec.waimaidemo.common.R;
import com.cqeec.waimaidemo.entity.Category;
import com.cqeec.waimaidemo.entity.Employee;
import com.cqeec.waimaidemo.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/category")
public class CategoryController {
    @Autowired
    CategoryService categoryService;
//    分页查询
@GetMapping("/page")
    public R<Page> pageR(int page, int pageSize){
    Page<Category> pageInfo=new Page<>(page,pageSize);
    LambdaQueryWrapper<Category> queryWrapper=new LambdaQueryWrapper<>();
//    根据字段sort升序
    queryWrapper.orderByAsc(Category::getSort);
    categoryService.page(pageInfo,queryWrapper);
    return R.success(pageInfo);
}
//添加数据
    @PostMapping
    public R<String> save(@RequestBody Category category){
        categoryService.save(category);
        return R.success("添加成功");
    }
    @GetMapping("/{id}")
    public R<Category> getById(@PathVariable("id")Long id){
        Category byId =categoryService.getById(id);
        return R.success(byId);
    }
    @PutMapping
    public R<String> update(@RequestBody Category category){
        categoryService.updateById(category);
        return R.success("修改成功");
    }
    @GetMapping("/list")
    public R<List<Category>> getList(Category category){
//        根据type查询数据
        LambdaQueryWrapper<Category> wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(category.getType()!=null,Category::getType,category.getType());
        wrapper.orderByAsc(Category::getSort).orderByDesc(Category::getUpdateTime);
        List<Category> list= categoryService.list(wrapper);
        return R.success(list);
    }
    @DeleteMapping
    public R<String> removeCategory(Long id){
    categoryService.deleteById(id);
    return R.success("删除成功");
    }
}

